<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <title>订单查询</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
  <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
  <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/font.css">
  <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/xadmin.css">
  <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
  <script type="text/javascript" src="${pageContext.request.contextPath}/static/lib/layui/layui.js" charset="utf-8"></script>
  <script type="text/javascript" src="${pageContext.request.contextPath}/static/js/xadmin.js"></script>
  <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
  <!--[if lt IE 9]>
  <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
  <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
  <![endif]-->
</head>

<body>
<div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="">首页</a>
        <a href="">订单</a>
        <a>
          <cite>查询</cite></a>
      </span>
  <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
    <i class="layui-icon" style="line-height:30px">ဂ</i></a>
</div>
<div class="x-body">
  <div class="layui-row">
    <form class="layui-form layui-col-md12 x-so" onsubmit="return false;">
      <input type="text" name="keywords"  placeholder="订单号" autocomplete="off" class="layui-input">
      <div class="layui-input-inline">
        <select id="status">
          <option value="">不限</option>
          <option value="1">已完成</option>
          <option value="2">等待收货</option>
        </select>
      </div>

      <input class="layui-input" placeholder="开始日" name="startTime" id="start">
      <input class="layui-input" placeholder="截止日" name="endTime" id="end">
      <button class="layui-btn" id="search" ><i class="layui-icon">&#xe615;</i></button>
    </form>
  </div>
  <xblock class="do-table">
    <button class="layui-btn layui-btn-danger" data-type="deleteAll"><i class="layui-icon"></i>批量删除</button>

  </xblock>
  <table id="data" lay-filter="contract"></table>


</div>
<script>


  var table;
  layui.use(['laydate','table'], function(){

    var laydate = layui.laydate;
    table = layui.table;
    //执行一个laydate实例
    laydate.render({
      elem: '#start' //指定元素
    });
    //执行一个laydate实例
    laydate.render({
      elem: '#end' //指定元素
    });

    table.render({
      elem: '#data'
      ,height: 600
      ,url: '${pageContext.request.contextPath}/order/select/list/data' //数据接口
      ,method: "post"
      ,page: true //开启分页
      ,cols: [[ //表头
        {type:'checkbox', fixed: 'left'}
        ,{field: 'id', title: 'ID', width:'5%', sort: true}
        ,{field: 'type', title: '订单类型', width:'10%'}
        ,{field: 'number', title: '订单号', width:'10%'}
        ,{field: 'commodity', title: '商品', width:'15%'}
        ,{field: 'customerId', title: '收货人', width: '10%', templet:function (d) {
            return d.customer.name;
          }}
        ,{field: 'mount', title: '订单金额', width:'10%'}
        ,{field: 'orderTime', title: '创建时间', width: '15%', sort: true}
        ,{field: 'status', title: '状态', width: '10%',templet:function (d) {
            if (d.status === 2){return "等待收货"}
            else if(d.status === 1){return "已完成"}
            }}
        ,{fixed: 'right',title: '操作', width:'10%', align:'center', toolbar: '#do'}
      ]]
    });
    //工具条事件
    table.on('tool(contract)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
      var data = obj.data; //获得当前行数据
      var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
      var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）


      if(layEvent === 'del'){ //删除
          layer.confirm('真的删除行么', function(index){

            layer.close(index);
            //向服务端发送删除指令
            var ids = [];
            ids.push(data.id)
            $.ajax({
              type:"post",
              url:"${pageContext.request.contextPath}/order/delete",
              data: {ids:ids},
              dataType: "json",
              success:function (res) {
                layer.msg(res.msg, {icon: 6},function () {
                  // 刷新父级的表格
                  table.reload('data');
                });
              }
            });

          });

      } else if(layEvent === 'edit'){ //编辑

          //do something
          x_admin_show('编辑合同','${pageContext.request.contextPath}/order/edit/page?id='+data.id)

      }else if(layEvent === 'LAYTABLE_TIPS'){
        layer.alert('Hi，头部工具栏扩展的右侧图标。');
      }
    });
    var $ = layui.$, active = {
      deleteAll: function(){
        var checkStatus = table.checkStatus('data')
                ,data = checkStatus.data;
        if (data.length === 0){
          layer.msg('请至少选中一行数据，进行删除！');
          return;
        }
        layer.confirm('确认要删除吗？',function(index){
          //获取选中数据中的id并组装成一个数组
          var ids = [];
          $(data).each(function () {

            ids.push(this.id);
          })
          // 发送请求到后端
          $.ajax({
            type:"post",
            url:"${pageContext.request.contextPath}/order/delete",
            data: {ids: ids},
            dataType: "json",
            success:function (res) {
              layer.msg(res.msg, {icon: 6},function () {
                // 刷新父级的表格
                table.reload('data');
              });
            }
          });
        });
      }
    };

    $('.do-table .layui-btn-danger').on('click', function(){
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });

    // 搜索按钮
    $("#search").click(function () {
      //获取对应的数据
      //获取状态id
      let status = $("#status").val();
      // 获取关键字
      var keywords = $("input[name='keywords']").val();
      // 获取开始时间
      var startTime = $("#start").val();
      // 获取结束时间
      var endTime = $("#end").val();
      // 组装总共的提交参数
      var param = {keywords: keywords,status:status,startTime: startTime ? startTime+" 00:00:00" : '', endTime: endTime ? endTime+" 23:59:59": ''}

      // 表格数据的重载
      table.reload("data",{where: param})
    });
  });

</script>


<script type="text/html" id="do">
  <a class="layui-btn layui-btn-xs" lay-event="edit">查看</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

</body>

</html>